﻿#include "cleaninfors.h"

#include <QApplication>
#include <QFile>
#include <QDebug>

CleanInfors  * CleanInfors::m_cleanInfors = NULL;
CleanInfors::CleanInfors(QObject *parent) : QObject(parent)
{
    initPin();
}

CleanInfors::~CleanInfors()
{

}

void CleanInfors::initPin()
{
    m_cleanList.clear();
    QString docParhFile =  QApplication::applicationDirPath();
    docParhFile +="/configs/cleaninfors.json";

    QFile files(docParhFile);
    if(files.open(QIODevice::ReadOnly)){
        QString inforsa = files.readAll();
        QJsonParseError errors;
        QJsonDocument doc = QJsonDocument::fromJson(inforsa.toUtf8(),&errors);
        #ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"doc=="<<doc;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"errors.error=="<<errors.error;
#endif
        if(errors.error == QJsonParseError::NoError){
            QJsonArray arrya = doc.array();
            for(int i = 0 ; i < arrya.size() ;i++ ){
                QJsonObject itema = arrya.at(i).toObject();
                #ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"itema=="<<itema;
#endif
                int indexs =  itema.take("indexs").toInt();
                QJsonArray cleans = itema.take("cleans").toArray();
                QList<QJsonObject> lista;
                for(int j = 0 ; j < cleans.size() ;j++ ){
                    QJsonObject itemb = cleans.at(j).toObject();
                    lista.append(itemb);
                }
                m_cleanList.insert(indexs,lista);
                if( itema.contains("versions")){
                    int versions =  itema.take("versions").toInt();

                    m_cleanIndexInfors.insert(indexs,versions);
                }else{
                    m_cleanIndexInfors.insert(indexs,0);
                }
            }
        }
        files.close();
    }
#ifdef DEBUG_INFOR
    qDebug()<<__func__<<__LINE__;
#endif
}

CleanInfors *CleanInfors::gestance()
{

    if(m_cleanInfors == NULL){
        m_cleanInfors= new CleanInfors();
    }
    return m_cleanInfors;
}

QList<int> CleanInfors::keyPins()
{
    return  m_cleanList.keys();
}

QList<QJsonObject> CleanInfors::getPin(int indexs)
{
    #ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_cleanList.keys()=="<<m_cleanList.keys();
#endif
    return  m_cleanList.value(indexs);
}

int CleanInfors::getVersion(int indexs)
{
    return  m_cleanIndexInfors.value(indexs);
}
